package com.dkm.orproject.dkmoa.service.serviceimpl;

import com.dkm.orproject.dkmoa.dao.OaClientTypeMapper;
import com.dkm.orproject.dkmoa.domain.OaClientType;
import com.dkm.orproject.dkmoa.service.OaClientTypeService;
import com.dkm.orproject.utils.utils.IdToobtainUtils;
import com.dkm.orproject.utils.utils.ThreeVo;
import com.dkm.orproject.utils.utils.Tools;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *  @ClassName : OaClientTypeServiceImpl  客户类型实现类
 *  @Description : OaClientTypeAction的service层实现类 //描述
 *  @Author : zmh //作者
 *  @Date: 2020-11-18 09:28  //时间
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OaClientTypeServiceImpl implements OaClientTypeService {
    /**
     * id生成工具
     */
    @Autowired
    IdToobtainUtils utils;

    /**
     * 客户类型mapper对象
     */
    @Resource
    OaClientTypeMapper oaClientTypeMapper;

    @Resource
    Tools tools;

    /**
     * 根据id删除客户类型
     *
     * @param pkClientTypeId id值
     * @return 回调函数
     */
    @Override
    public int deleteByPrimaryKey(Long pkClientTypeId) {
        return oaClientTypeMapper.deleteByPrimaryKey(pkClientTypeId);
    }

    /**
     * 插入客户类型
     *
     * @param oaClientType 客户类型模型对象
     * @return 回调函数
     */
    @Override
    public int insertSelective(OaClientType oaClientType) {
        oaClientType.setPkClientTypeId(utils.getNumberId());
        if(ObjectUtils.isEmpty(oaClientType.getClientTypeSuperior())){
            oaClientType.setClientTypeSuperior(0L);
        }
        return oaClientTypeMapper.insertSelective(oaClientType);
    }

    /**
     * 根据id查询客户类型
     *
     * @param pkClientTypeId 客户类型id
     * @return 客户类型对象模型
     */
    @Override
    public OaClientType selectByPrimaryKey(Long pkClientTypeId) {
        return oaClientTypeMapper.selectByPrimaryKey(pkClientTypeId);
    }

    /**
     * 修改客户类型对象
     *
     * @param oaClientType 客户类型对象
     * @return 回调函数
     */
    @Override
    public int updateByPrimaryKeySelective(OaClientType oaClientType) {
        return oaClientTypeMapper.updateByPrimaryKeySelective(oaClientType);
    }

    /**
     * 客户类型总记录数
     *
     * @param oaClientType 客户类型模型对象
     * @param startTime    开始时间
     * @param endDate      结束时间
     * @return 总行数
     */
    @Override
    public Long rowCount(OaClientType oaClientType, String startTime, String endDate) {
        return oaClientTypeMapper.rowCount(oaClientType, startTime, endDate);
    }

    /**
     * 分页查询客户类型
     *
     * @param oaClientType 客户类型模型对象
     * @param startTime    开始时间
     * @param endDate      结束时间
     * @param page         当前页数
     * @param limit        每页行数
     * @return 客户类型的集合
     */
    @Override
    public List<OaClientType> selectAll(OaClientType oaClientType, String startTime, String endDate, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return oaClientTypeMapper.selectAll(oaClientType, startTime, endDate);
    }

    /**
     * 批量删除客户
     *
     * @param list 客户id集合
     * @return 回调函数
     */
    @Override
    public int deleteAll(List<Long> list) {
        return oaClientTypeMapper.deleteAll(list);
    }



    @Override
    public List<ThreeVo> loadOaClientType(Long oaClientTypeId, String show) {
        //通过页面传过来的id值查询客户类型对象
        OaClientType clientType = oaClientTypeMapper.selectByPrimaryKey(oaClientTypeId);
        //定义回调客户类型集合
        List<OaClientType> oaClientTypes;
        if(StringUtils.isEmpty(show)){
            oaClientTypes = oaClientTypeMapper.loadOaClientType(oaClientTypeId);//修改 过滤自己
        }else {
            oaClientTypes = oaClientTypeMapper.loadOaClientType(null); // 查看 查询所有
        }
        List<ThreeVo> threeVos = new ArrayList<>();
        for (OaClientType oaClientType:oaClientTypes) {
            ThreeVo threeVo = new ThreeVo();
            if("1".equals(show)){
                if(clientType!=null && clientType.getClientTypeSuperior().equals(oaClientType.getPkClientTypeId())){
                    threeVo.setSelected(true);
                }
            }else if("2".equals(show)){
               if( clientType!=null && clientType.getPkClientTypeId().equals(oaClientType.getPkClientTypeId())){
                threeVo.setSelected(true);
             }
            }
            threeVo.setValue(oaClientType.getPkClientTypeId());
            threeVo.setName(oaClientType.getClientTypeName());
            threeVo.setSuperId(oaClientType.getClientTypeSuperior());
            threeVos.add(threeVo);
        }
        return tools.createTree(threeVos);
    }



}
